package eu.goldenak.ircbot.module;

import eu.goldenak.ircbot.IrcBot;
import eu.goldenak.ircbot.ModuleManager;

public class Modules extends ModuleBase {
	
	public void onChannelPrivmsg (IrcBot pBot, String sChannel, String sNickname, String sMessage) {
		sMessage = sMessage.trim ();
		
		if (sMessage.charAt (0) != '!') {
			return;
		}
		
		if (sMessage.startsWith ("!loadmodule")) {
			if (sMessage.length () == 11) {
				pBot.out.privmsg (sChannel, "Usage: !loadmodule ModuleName");
			} else {
				String sModuleName = sMessage.substring (12);
				ModuleManager pModMan = ModuleManager.getInstance ();
				
				if (pModMan.isModuleLoaded (sModuleName)) {
					pBot.out.privmsg (sChannel, "The module '" + sModuleName +
						"' is already loaded. Unload first using " +
						"!unloadmodule.");
				} else {
					if (pModMan.loadModule (sModuleName)) {
						pBot.out.privmsg (sChannel, "Module '" + sModuleName +
							"' successfully loaded.");
					} else {
						pBot.out.privmsg (sChannel, "Module '" + sModuleName +
							"' could not be loaded.");
					}
				}
			}
		}
		else if (sMessage.startsWith ("!unloadmodule")) {
			if (sMessage.length () == 13) {
				pBot.out.privmsg (sChannel, "Usage: !unloadmodule ModuleName");
			} else {
				String sModuleName = sMessage.substring (14);
				ModuleManager pModMan = ModuleManager.getInstance ();
				
				if (!pModMan.isModuleLoaded (sModuleName)) {
					pBot.out.privmsg (sChannel, "There is no module '" + 
						sModuleName + "' loaded at the moment. Load first " +
						"using !loadmodule.");
				} else {
					pModMan.unloadModule (sModuleName);
					pBot.out.privmsg (sChannel, "Module '" + sModuleName +
						"' unloaded.");
				}
			}
		}
	}
}